//#include <iostream>
#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;
typedef struct LNode {
    ElemType data;
    LNode *next;
} LNode, *Linklist;

Linklist GetElem(Linklist L, ElemType pos) {
    int i = 0;
    if (pos < 0) return NULL;
    while (L && i < pos) {
        L = L->next;
        i++;
    }
    return L;
}

void List_head_insert(Linklist &L) {
    Linklist p, q;
    ElemType num;
    L = (Linklist) malloc(sizeof(LNode));
    q = L;
    scanf("%d", &num);
    while (num != 9999) {
        p = (Linklist) malloc(sizeof(LNode));
        p->data = num;
        q->next = p;
        q = p;
        scanf("%d", &num);
    }
    p->next = NULL;
}

bool ListFrontInsert(Linklist L, ElemType Insertpos, ElemType num) {
    Linklist p = GetElem(L, Insertpos - 1);
    if (p == NULL) {
        return false;
    }
    Linklist q;
    q = (Linklist) malloc(sizeof(LNode));
    q->data = num;
    q->next = p->next;
    p->next = q;
    return true;
}

//链表输出
void Print_list(Linklist L) {
    L = L->next;
    while (L) {
        printf("%3d ", L->data);
        L = L->next;
    }
}

//往第i个位置插入元素实战
int main() {
    Linklist L;
    Linklist search;
    List_head_insert(L);
//    //按序号查找
//    search = GetElem(L, 2);
//    if (search != NULL) {
//        //serial number:编号,序号
//        printf("Succeeded in searching by serial number\n");
//        printf("%3d", search->data);
//    }
    bool flag;
    //新结点插入到第i个位置
    flag = ListFrontInsert(L, 2, 100);
    Print_list(L);
    return 0;
}
